import Vuex from 'vuex'
import Vue from 'vue'
import { nanoid } from 'nanoid'
import System from '../classes/System'
Vue.use(Vuex)
// 这里是用户共享数据
const userAbout = {
    namespaced: true,
    state: {
        token: localStorage.getItem('ytoken'),
        user: JSON.parse(localStorage.getItem('yuser')),
        CONSTANT: {
            PASSWORD_NOT_VALIDATE: '密码长度过短',
            USERNAME_NOT_VALIDATE: '用户名长度过短',
            PASSWORD_NOT_SAME: '两次输入密码不一致'
        },
        userPersonality:JSON.parse(localStorage.getItem('yPer'))
    },
    mutations: {
        token_update(state, token) {
            //console.log('token更新')
            document.cookie = `yToken=${token}`
            localStorage.setItem('ytoken', token)
            state.token = token
        },
        user_update(state, user) {
            //
            localStorage.setItem('yuser', JSON.stringify(user))
            state.user = user
        },
        per_update(state,per){
            localStorage.setItem('yPer',JSON.stringify(per))
            state.userPersonality = per
        }
    }
}
const clipBoardAbout = {
    namespaced:true,
    actions:{
        
    }
    ,
    state:{
      // 剪切板是个数组 目前有两种类型 代码编辑器里面使用ctrl+c 文件管理器里面复制和粘贴文件
      clip:new Array()
    }
}
const appAbout = {
    namespaced: true,
    actions: {
        openApp(context, app_id) {
            // 创建id
            let process_id = nanoid().replace(/[0-9-]/g)
            // 创建虚拟进程
            const process = {
                app_id, // 对应的app
                process_id,
                isOpen: true, //默认打开
                priority: 1, // 窗口显示优先级
                create_time: new Date(), // 进程创建时间
                isLoading: false, // 加载状态
                meta:null
            }
            context.commit('OPENAPP', process)
        },
        openOtherApp(context,app_id){
                // 创建id
                let process_id = nanoid().replace(/[0-9-]/g)
                // 创建虚拟进程
                const process = {
                    app_id, // 对应的app
                    process_id,
                    isOpen: true, //默认打开
                    priority: 1, // 窗口显示优先级
                    create_time: new Date(), // 进程创建时间
                    isLoading: false, // 加载状态
                    meta:null
                }
                context.commit('OPENOTHERAPP', process)
        },
        openAppWithData(context,data) {
           // console.log(data)
            const app_id = data.app_id
            const meta = data.meta
            // 根据data进行打开app的附加数据
            let process_id = nanoid().replace(/[0-9-]/g)
            const process = {
                app_id, // 对应的app
                process_id,
                isOpen: true, //默认打开
                priority: 1, // 窗口显示优先级
                create_time: new Date(), // 进程创建时间
                isLoading: false, // 加载状态
                meta // 额外数据
            }
            context.commit('OPENAPP',process)
        },
        closeFrame(context, proccess) {
            context.commit('CLOSEFRAME', proccess)
        }
    },
    mutations: {
        OPENAPP(state, process) {
            // 优先级的改变
            //console.log(process)
            const priority = state.max_priority + 100
            process.priority = priority
            state.max_priority = process.priority
            // 创建进程
            // console.log(process.process_id)
            Vue.set(state.apps[process.app_id].processes, process.process_id, process)
            state.apps[process.app_id].processes.num += 1
        },
        OPENOTHERAPP(state,process){
            const priority = state.max_priority + 100
            process.priority = priority
            state.max_priority = process.priority
            // 创建进程
            // console.log(process.process_id)
            Vue.set(state.otherApps[process.app_id].processes, process.process_id, process)
            state.otherApps[process.app_id].processes.num += 1
        },
        CLOSEFRAME(state, process) {
            if(state.apps[process.app_id]!=undefined){
            Vue.delete(state.apps[process.app_id].processes, process.process_id)
            state.apps[process.app_id].processes.num -= 1
            }else{
                Vue.delete(state.otherApps[process.app_id].processes, process.process_id)
                state.otherApps[process.app_id].processes.num -= 1
            }
            
        },
        FOUCUSAPP(state, process) {
            // app窗口优先级的提升
            const priority = state.max_priority + 2
            process.priority = priority
            state.max_priority = priority
        },
        CLOSEALLFRAMES(state, app_id) {
            // 删除所有frame
            for (let i in state.apps[app_id].processes) {
                if (i != 'num') {
                    Vue.delete(state.apps[app_id].processes, i)
                    state.apps[app_id].processes.num -= 1
                }
            }
        }
    },
    getters: {
        allProcesses(state) {
            const res = new Array()
            for (let key in state.apps) {
                for (let p in state.apps[key].processes) {
                    if (p != 'num') {
                        res.push(state.apps[key].processes[p])
                    }
                }
            }
            for(let key in state.otherApps){
                for (let p in state.otherApps[key].processes) {
                    if (p != 'num') {
                        res.push(state.otherApps[key].processes[p])
                    }
                }
            }
            return res
        }
    },
    state: {
        apps: {
            setting: {
                app_id: 'setting',
                img: '../assets/setting.png',
                processes: {
                    num: 0 
                }
            },
            'folder-manager': {
                app_id: 'folder-manager',
                img: '../assets/folder-manager.png',
                processes: {
                    num: 0
                }
            },
            'text-editor': {
                app_id: 'text-editor',
                img: '../assets/editor.png',
                processes: {
                    num: 0
                }
            },
            'markdown-editor':{
                app_id: 'markdown-editor',
                img: '../assets/file_md.png',
                processes: {
                    num: 0
                }
            },
            'pdf-viewer':{
                app_id:'pdf-viewer',
                img:'../assets/file_pdf.png',
                processes:{
                    num:0
                }
            },
            'yterm':{
                app_id:'yterm',
                img:'../assets/yterm.png',
                processes:{
                    num:0
                }
            },
            'manage':{
                app_id:'manage',
                img:'../assets/manage.png',
                processes:{
                    num:0
                }
            }
        },
        max_priority: 2020,
        otherApps:{
            'notice-view':{
                app_id:'notice-view',
                processes:{
                    num:0
                }
            }
        }
    }
}
const store = new Vuex.Store({
    modules: {
        userAbout,
        appAbout
    }
})
export default store